package structures.grammar;

import structures.table.symboltable.ItemInfo;

/**
 *
 * @author Slayer
 */
public class Rule implements Cloneable {

    private ItemInfo from;          //right part of the rule - not terminal
    private ItemList to;        //left part of the rule - set of terminals and
    //not terminals

    public ItemList getTo() {
        return to;
    }

    public ItemInfo getFrom() {
        return from;
    }

    public Rule(ItemInfo from, ItemList to) {
        this.from = from;
        this.to = to;
    }

    public int length() {
        return to.size();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Rule other = (Rule) obj;
        if (this.from != other.from && (this.from == null || !this.from.equals(other.from))) {
            return false;
        }
        if (this.to != other.to && (this.to == null || !this.to.equals(other.to))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 37 * hash + (this.from != null ? this.from.hashCode() : 0);
        hash = 37 * hash + (this.to != null ? this.to.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();

        builder.append(from.toString());
        builder.append(" -> ");
        builder.append(to.toString());

        return builder.toString();
    }

    @Override
    public Object clone() {
        return new Rule((ItemInfo) from.clone(), (ItemList) to.clone());
    }
}
